package modifyx

import (
	. "gitee.com/zhongguo168a/gocodes/datax/floatx"
)

type IBonus interface {
	Key() string
	Factor() float64
	Multiplier() float64
	Bonus() float64
	Valid() bool
}

func NewBonusAll(key string, bonus, factor, multiplier float64) (obj *Bonus) {
	obj = &Bonus{
		key:        key,
		bonus:      bonus,
		factor:     factor,
		multiplier: multiplier,
	}
	return
}
func NewBonus(key string) (obj *Bonus) {
	obj = &Bonus{
		key:        key,
		multiplier: 1.0,
	}
	return
}

type Bonus struct {
	// 例如: CEffectDamage/Amount
	key string
	// 倍数计算后的加成
	bonus float64
	// 系数
	factor float64
	// 倍数
	multiplier float64 `value:"1"`
}

func (a *Bonus) Valid() bool {
	return true
}

func (a *Bonus) Combine(b IBonus) {
	a.factor += b.Factor()
	a.bonus += b.Bonus()
	a.multiplier *= b.Multiplier()
}

func (b *Bonus) Key() string {
	return b.key
}

func (b *Bonus) Factor() float64 {
	return b.factor
}

func (b Bonus) Multiplier() float64 {
	return b.multiplier
}

func (b Bonus) Bonus() float64 {
	return b.bonus
}

func (b *Bonus) SetKey(val string) *Bonus {
	b.key = val
	return b
}

func (b *Bonus) SetFactor(val float64) *Bonus {
	b.factor = val
	return b
}

func (b *Bonus) SetMultiplier(val float64) *Bonus {
	b.multiplier = val
	return b
}

func (b *Bonus) SetBonus(val float64) *Bonus {
	b.bonus = val
	return b
}

// y = x * (1 + Factor)* Multiplier + Bonus
func CalculateBonusFactor(x float64, a IBonus, useFactor, useMultiplier, useBonus bool) (y float64) {
	y = x

	if a == nil {
		return
	}

	factor := a.Factor()
	// a.Factor != 0
	if Neq(factor, 0) && useFactor == true {
		y = x * (1 + factor)
	}

	multiplier := a.Multiplier()
	// a.Multiplier != 1
	if Neq(multiplier, 1) && useMultiplier == true {
		y = y * multiplier
	}

	bonus := a.Bonus()
	// a.Bonus != 0
	if Neq(bonus, 0) && useBonus == true {
		y = y + bonus
	}

	return
}
